try {
	Type.registerNamespace('NCMMDBUtil');

	window.uploadTotal;
	window.uploadSum;
	window.showLoading = true;

	NCMMDBUtil.testNCConnection = function(callback) {
		//拼装HTTP请求参数
		var httpURL = NCMMHttpUtil.getQuestURL(NCMMHttpUtil.LOGIN);
		var httpparam = NCMMHttpUtil.getHTTPRequestParam("", "", null);

		//发送HTTP请求	
		NCMMHttpUtil.sendHTTPRequest(httpURL, httpparam, function(data, issuccess) {
			if(issuccess > 0) {
				if(issuccess == 1) { //成功		
					if($isJSONObject(data) && data["return"]["returnCode"] == "1111") {
						if(callback != null && typeof(callback) == 'function') {
							callback(true, "");
						}
						return;
					} else {
						//afterError("服务器连接", data); //成功但登陆错误
						if(callback != null && typeof(callback) == 'function') {
							callback(false, data);
						}
						return;
					}
				} else {
					//afterError("服务器连接", "未知错误"); //成功但记录为空
					if(callback != null && typeof(callback) == 'function') {
						callback(false, "服务器连接");
					}
					return;
				}
			} else { //失败
				//				$alert("服务器连接错误 ");
				if(callback != null && typeof(callback) == 'function') {
					callback(false, "服务器连接错误 ");
				}
				return;
			}
		});
	};

	//请求NC端单据数据
	/*
		param:
			VOClassHead: 主表单据类型
			VOClassBody: 子表单据类型，单表时为空
			srvname: 请求接口名称
			condition: 查询条件
		return:
			单据记录json对象
	*/
	NCMMDBUtil.loadNCBaseData = function(VOClassHead, VOClassBody, srvname, condition, callback) {
		//初始化对照表 -- 将来要挪到登录成功后
		//		NCMMDBUtil.initMappingTable();

		//获取外部系统调用标识
		var docType = getNCBillNode(VOClassHead);
		if(docType == "") {
			$alert("该单据不支持下载！");
			if(callback != null && typeof(callback) == 'function') {
				callback(null);
			}
			return;
		}

		//拼装HTTP请求参数
		var httpURL = NCMMHttpUtil.getQuestURL(srvname);

			//	$alert("httpURL=" + $jsonToString(httpURL));
		var aggvo = "aggvo";
		if(VOClassBody == null)
			aggvo = ""; //单表

		var httpparam = NCMMHttpUtil.getHTTPRequestParam(docType, aggvo, condition);
		//		$alert("httpparam=" + $jsonToString(httpparam));

		//发送HTTP请求	
		NCMMHttpUtil.sendHTTPRequest(httpURL, httpparam, function(data, issuccess) {
			if(issuccess > 0) {
				if(issuccess == 1) { //成功
					if(callback != null && typeof(callback) == 'function') {
											//	$alert("data=" + $jsonToString(data));
						callback(data);
					}
				} else {
					//afterError("下载", "NC端没有返回单据记录"); //成功但记录为空
					if(callback != null && typeof(callback) == 'function') {
						callback(data);
						//callback(null);
					};
				}
			} else { //失败
				afterError("下载", data);
				if(callback != null && typeof(callback) == 'function') {
					callback(null);
				};
			}
		});
	};

	
	//请求NC端单据数据
	//from wjw
	NCMMDBUtil.loadNCBaseData2 = function(VOClassHead, VOClassBody, srvname, condition, callback, error) {
		//初始化对照表 -- 将来要挪到登录成功后
		//		NCMMDBUtil.initMappingTable();

		//获取外部系统调用标识
		var docType = getNCBillNode(VOClassHead);

		//拼装HTTP请求参数
		var httpURL = NCMMHttpUtil.getQuestURL(srvname);

		//$alert("httpURL=" + $jsonToString(httpURL));
		var aggvo = "aggvo";
		if(VOClassBody == null)
			aggvo = ""; //单表

		var httpparam = NCMMHttpUtil.getHTTPRequestParam(docType, aggvo, condition);
//		$alert("httpparam=" + $jsonToString(httpparam));
		var tablename = VOClassHead.prototype.getTableName();
		// 请求数据并下载
		NCMMHttpUtil.servicePost(httpURL, httpparam, callback, error, tablename);

	};

	//获取VO数据记录数
	NCMMDBUtil.getBillCount = function(vos) {
		var vocount = 0;
		if($isJSONObject(vos) == false) {
			//$alert("vos isnot json");
			return vocount;
		}

		vocount = parseInt((vos.pageinfos).totalRows);
		return vocount;
	};

	//获取外系统调用标识
	function getNCBillNode(VOClass) {
		var ncbillcode = "";
		//		$alert($jsonToString(VOClass.prototype.getTableName()));
		var queryCondition = {
			"apptablename": VOClass.prototype.getTableName()
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, queryCondition, function(data) {
			var json = $stringToJSON(data);
			if(json.length <= 0) {
				ncbillcode = "";
			} else {
				ncbillcode = json[0].ncbillcode;
				//$alert("外系统调用标识： " + ncbillcode);
			}
		});

		return ncbillcode;
	}

	function afterError(errortype, data) {
		$alert(errortype + "错误: " + $jsonToString(data));
	}

	//清除本地库表中的原有记录
	NCMMDBUtil.clearAllOldData = function(VOClassHead, VOClassBody) {
		if(VOClassBody != null)
			NCMMPubUtil.dropTable(VOClassBody);
		NCMMPubUtil.dropTable(VOClassHead);
		if(VOClassHead.prototype.getPrimaryAttr() == "_id") {
			NCMMPubUtil.createTableWithAutoPK(VOClassHead);
		} else {
			NCMMPubUtil.createTable(VOClassHead);
		}
		if(VOClassBody != null) {
			if(VOClassBody.prototype.getPrimaryAttr() == "_id") {
				NCMMPubUtil.createTableWithAutoPK(VOClassBody);
			} else {
				NCMMPubUtil.createTable(VOClassBody);
			}
		}
	};

	//请求返回值插入本地库表
	NCMMDBUtil.addNewDatas = function(VOClassHead, VOClassBody, vos) {
		if(VOClassBody != null) {
			return addNewDatasAggvo(VOClassHead, VOClassBody, vos);
		}

		//单表插入		
		var retCount = 0;
		var billinfos = $stringToJSON(vos.billinfos);
		//		for(var i = 0; i < billinfos.length; i++) {
		//			//			var vo = fillAttributes(VOClassHead, billinfos[i].billinfo);
		//			//插入表
		//			NCMMPubUtil.insertVO(VOClassHead, billinfos[i].billinfo);
		//			retCount++;
		//		}

		//为了优化性能，改单条记录插入为批量插入

		retCount = NCMMPubUtil.insertBatchVO(VOClassHead, billinfos);

		return retCount;
	};

	//请求返回值插入本地库表
	NCMMDBUtil.addNewDatas2 = function(VOClassHead, VOClassBody, vos) {
		if(VOClassBody != null) {
			return addNewDatasAggvo(VOClassHead, VOClassBody, vos);
		}

		//单表插入		
		var retCount = 0;
		//		for(var i = 0; i < billinfos.length; i++) {
		//			//			var vo = fillAttributes(VOClassHead, billinfos[i].billinfo);
		//			//插入表
		//			NCMMPubUtil.insertVO(VOClassHead, billinfos[i].billinfo);
		//			retCount++;
		//		}

		//为了优化性能，改单条记录插入为批量插入	

		retCount = NCMMPubUtil.insertBatchVO(VOClassHead, vos);

		return retCount;
	};

	//插入主子表数据
	function addNewDatasAggvo(VOClassHead, VOClassBody, vos) {
		var retCount = 0;
		//		$alert("vos="+$jsonToString(vos));
		var headvos = vos.billinfos;
		var docHeadvos = new Array();
		var docItemvos = new Array();

		for(var i = 0; i < headvos.length; i++) {
			//表头
			var aBillInfo = headvos[i];
			if(aBillInfo)
				docHeadvos.push(aBillInfo["billinfo"]["head"]);
			//表体
			var aItems = aBillInfo["billinfo"]["items"];
			if($isJSONArray(aItems)) {
				for(var j = 0; j < aItems.length; j++) {
					docItemvos.push(aItems[j]["item"]);
				}
			}
		}
		//		$alert($jsonToString({"docHeadvos":docHeadvos}));
		//		$alert($jsonToString({"docItemvos":docItemvos}));

		var retCount = 0; //主表插入记录数
		//为了优化性能，改单条记录插入为批量插入
		NCMMPubUtil.insertBatchVO(VOClassBody, docItemvos);
		retCount = NCMMPubUtil.insertBatchVO(VOClassHead, docHeadvos);

		return retCount;

	}

	//提交本地数据到NC端
	/*
		param:
			ncbillcode: 外系统调用标识
			billinfos: 待提交数据（JSON数组）
		return:
			json对象
			调用成功后的返回值示例：
			{
				returnCode: "1111",
				returnDesc: "成功",
				returnFlag: 1			//反馈标识：0表示失败（失败时原因写入returnDesc），1表示成功，3表示已经回传即重复回传了
			}
	*/
	NCMMDBUtil.submitLocaldataToNC = function(ncbillcode, billinfos, callback) {
		//拼装HTTP请求参数
		var httpURL = NCMMHttpUtil.getQuestURL(NCMMHttpUtil.BILLUPDATE);

		var httpparam = NCMMHttpUtil.getHTTPSubmitParam(ncbillcode, billinfos);
		//$alert("httpparam=" + $jsonToString(httpparam));

		//发送HTTP请求	
		NCMMHttpUtil.sendHTTPRequest(httpURL, httpparam, function(data, issuccess) {
			if(issuccess > 0) {
				if(issuccess == 1) { //成功
					if(callback != null && typeof(callback) == 'function') {
						callback(data);
					}
				} else {
					if(callback != null && typeof(callback) == 'function') {
						callback(data);
					}
				}
			} else { //失败
				afterError("上传", data);
				if(callback != null && typeof(callback) == 'function') {
					callback(null);
				};
			}
		});
	};

	//更新本地app_mappinghead表中下载记录数和下载时间
	NCMMDBUtil.updateMappingTable = function(VOClass, downloadnum, downloadtime, progress) {
		var condition = {
			"datatype": "BD",
			"apptablename": VOClass.prototype.getTableName()
		};

		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var singledata = (jsondata.bdlistdata)[0];
			singledata["downloadnum"] = downloadnum;
			singledata["downloadtime"] = downloadtime;
			singledata["progressbar"] = (progress == null) ? "100" : progress;
			//						$alert($jsonToString({"singledata":singledata}));
			NCMMPubUtil.updateVO(AppmappingheadVO, singledata);
		});
	};

	//根据表名获得VO
	NCMMDBUtil.getDataVOByTablename = function(tablename) {
		if(FactoryVO.prototype.getTableName() == tablename) {
			return FactoryVO;
		}
		if(StockorgVO.prototype.getTableName() == tablename) {
			return StockorgVO;
		}
		if(StordocVO.prototype.getTableName() == tablename) {
			return StordocVO;
		}
		if(MaterialVO.prototype.getTableName() == tablename) {
			return MaterialVO;
		}
		if(BatchcodeVO.prototype.getTableName() == tablename) {
			return BatchcodeVO;
		}
		if(MeasdocVO.prototype.getTableName() == tablename) {
			return MeasdocVO;
		}
		if(BDWkVO.prototype.getTableName() == tablename) {
			return BDWkVO;
		}
		if(DeptVO.prototype.getTableName() == tablename) {
			return DeptVO;
		}
		if(BarCodeDictVO.prototype.getTableName() == tablename) {
			return BarCodeDictVO;
		}
		if(BarCodeDictBodyVO.prototype.getTableName() == tablename) {
			return BarCodeDictBodyVO;
		}
		if(BarCodeRuleVO.prototype.getTableName() == tablename) {
			return BarCodeRuleVO;
		}
		if(BarCodeRuleBodyVO.prototype.getTableName() == tablename) {
			return BarCodeRuleBodyVO;
		}
		if(CustomerVO.prototype.getTableName() == tablename) {
			return CustomerVO;
		}
		if(SupplierVO.prototype.getTableName() == tablename) {
			return SupplierVO;
		}
		if(RackVO.prototype.getTableName() == tablename) {
			return RackVO;
		}
		if(StoreStateVO.prototype.getTableName() == tablename) {
			return StoreStateVO;
		}
	};

	NCMMDBUtil.hideLoading = function() {
		//		window.uploadTotal = window.uploadTotal + 1;
		//		$alert("uploadSum=" + (window.uploadSum).toString() + "；uploadTotal=" + (window.uploadTotal).toString());
		//		if(window.uploadTotal == window.uploadSum) {
		//			$app.hideLoadingBar();
		//		}
		window.uploadSum = window.uploadSum - 1;
		if(window.uploadSum <= 0) {
			$app.hideLoadingBar();
		}
	};

	//调用扩展服务提供的NC数据下载（适配大数据）
	//param:
	//VOClass：数据VO
	//appbillname：档案/单据名称
	//condition：过滤条件
	//groupings：分组信息，初始值为[]
	//index：组序，初始值为0
	//callback：事件结束后的回调方法，由调用js提供
	//needReloadrow：是否需要刷新界面，如果需要，刷新方法由调用js提供
	NCMMDBUtil.processDocDataFromNC2 = function(VOClass, appbillname, condition, groupings, index, callback, needReloadrow) {
		if(index == 0) {
			if(window.showLoading) {
				$app.showLoadingBar({
					msg: "正在下载……"
				});
				window.showLoading = false;
			}
			// 清空本地表记录
			NCMMDBUtil.clearAllOldData(VOClass, null);
		} else {
			condition["startRowNumber"] = Number(groupings[index][0]);
			condition["endRowNumber"] = Number(groupings[index][1]);
		}

		// 从NC端获取档案数据
		NCMMDBUtil.loadNCBaseData2(VOClass, null, NCMMHttpUtil.BDDOC, condition, function(caller, data) {
			// 获得档案数据	
			var result = $stringToJSON(($stringToJSON(data.http_result).response || {}));
			//			$alert("result=" + $jsonToString(result));
			if(result != null) {
				// 如果没有信息直接返回
				var billinfos = result.billinfos || {};
				var props = Object.getOwnPropertyNames(billinfos) || [];
				if(props.length == 0) {
					if($isJSONObject(result)) {
						NCMMDBUtil.hideLoading();
						$toast("没有下载到【" + appbillname + "】");
					} else {
						NCMMDBUtil.hideLoading();
						$toast(result);
					}
					return;
				}
				// 获得条数
				var sum = NCMMDBUtil.getBillCount(result);
				//$alert("sum=[" + sum + "]:" + (result.pageinfos).startRowNumber + "~~" + (result.pageinfos).endRowNumber);
				// 第一次获得下载分组
				if(index == 0) {
					groupings = NCMMDBUtil.getGroup(sum, 5000);
				}
				// TODO 执行拓展服务insert操作
				var attrs = VOClass.prototype.getAllAttrs();
				var fields = attrs.map(function(row, i, arr) {
					return row.name;
				});

				var valueMapList = Array.prototype.map.call(result.billinfos, function(row, i, arr) {
					var data = $stringToJSON((row || {}));
					var values = attrs.map(function(attr, i, array) {
						var fname = attr.name;
						if(!!data["billinfo"][fname]) {
							if(data["billinfo"][fname] != "" && typeof(data["billinfo"][fname]) == "string") {
								data["billinfo"][fname] = NCMMPubUtil.htmlspecialchars(data["billinfo"][fname]);
							}
							return data["billinfo"][fname];
						} else {
							if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
								return 0;
							} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
								return 0.0;
							} else {
								return "";
							}
						}
					});
					return values;
				});
				//												$alert("valueMapList=" + $jsonToString(valueMapList));
				$app.replaceBatch({
					db: NCMMPubUtil.DatabaseName,
					table: VOClass.prototype.getTableName(),
					fields: fields,
					values: valueMapList,
					callback: function(args_null, args) {
						if(args.code == '1000' || args.code == 1000) {
							var currentDate = NCMMHttpUtil.getCurrentLocalTime();
							//3. 更新本地app_mappinghead表中下载记录数和下载时间
							NCMMDBUtil.updateMappingTable(VOClass, groupings[index][1], currentDate, String(groupings[index][2]));
							//4. 刷新页面显示数据
							if(needReloadrow && needReloadrow == true) {
								reloadSingleRow(VOClass);
							}

							var maxLength = groupings.length;
							var i = index + 1;
							if(i != groupings.length) {
								(function(VOClass, appbillname, condition, groupings, i, callback, needReloadrow) {
									NCMMDBUtil.processDocDataFromNC2(VOClass, appbillname, condition, groupings, i, callback, needReloadrow);
								})(VOClass, appbillname, condition, groupings, i, callback, needReloadrow);
							} else {
								NCMMDBUtil.hideLoading();
								$toast("【" + appbillname + "】下载了【" + sum + "】条数据。");
								if(callback) {
									callback();
								}
							}
						} else {
							NCMMDBUtil.hideLoading();
							$js.toast("【" + appbillname + "】下载异常");
							if(callback) {
								callback();
							}
						}
					}
				});
			}
		}, function(_NULL, args) {
			NCMMDBUtil.hideLoading();
			$js.toast('下载数据出错！');
			if(callback) {
				callback();
			}
		});
	};

	//主子结构的NC数据下载（适配大数据）
	NCMMDBUtil.processDocDataFromNC3 = function(VOClassHead, VOClassBody, appbillname, condition, callback, needReloadrow) {
		if(window.showLoading) {
			$app.showLoadingBar({
				msg: "正在下载……"
			});
			window.showLoading = false;
		}
		// 清空本地表记录
		NCMMDBUtil.clearAllOldData(VOClassHead, VOClassBody);

		// 从NC端获取档案数据
		NCMMDBUtil.loadNCBaseData(VOClassHead, VOClassBody, NCMMHttpUtil.BDDOC, condition, function(data) {
			// 获得档案数据	
			var result = ($stringToJSON(data) || {});
			//			$alert("result=" + $jsonToString(result));
			if(result != null) {
				// 如果没有信息直接返回
				var billinfos = result.billinfos || {};
				var props = Object.getOwnPropertyNames(billinfos) || [];
				if(props.length == 0) {
					if($isJSONObject(result)) {
						NCMMDBUtil.hideLoading();
						$toast("没有下载到【" + appbillname + "】");
					} else {
						NCMMDBUtil.hideLoading();
						$toast(result);
					}
					return;
				}
				// 获得条数
				var sum = NCMMDBUtil.getBillCount(result);
				//				$alert("sum=[" + sum + "]:" + (result.pageinfos).startRowNumber + "~~" + (result.pageinfos).endRowNumber);
				// TODO 执行拓展服务insert操作
				var attrs_head = VOClassHead.prototype.getAllAttrs();
				var attrs_body = VOClassBody.prototype.getAllAttrs();
				var fields_head = attrs_head.map(function(row, i, arr) {
					return row.name;
				});
				var fields_body = attrs_body.map(function(row, i, arr) {
					return row.name;
				});

				var valueMapList_head = Array.prototype.map.call(result.billinfos, function(row, i, arr) {
					var data = $stringToJSON((row || {}));
					var values = attrs_head.map(function(attr, i, array) {
						var fname = attr.name;
						if(!!data["billinfo"]["head"][fname]) {
							if(data["billinfo"]["head"][fname] != "" && typeof(data["billinfo"]["head"][fname]) == "string") {
								data["billinfo"]["head"][fname] = NCMMPubUtil.htmlspecialchars2sqliteex(data["billinfo"]["head"][fname]);
							}
							return data["billinfo"]["head"][fname];
						} else {
							if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
								return 0;
							} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
								return 0.0;
							} else {
								return "";
							}
						}
					});
					return values;
				});

				//				$alert("valueMapList_head=" + $jsonToString(valueMapList_head));
				var valueMapList_body = [];
				for(var i = 0; i < result.billinfos.length; i++) {
					for(var j = 0; j < (((result.billinfos)[i])["billinfo"]["items"]).length; j++) {
						var data = (((result.billinfos)[i])["billinfo"]["items"])[j];
						var values = attrs_body.map(function(attr, i, array) {
							var fname = attr.name;
							if(!!data["item"][fname]) {
								if(data["item"][fname] != "" && typeof(data["item"][fname]) == "string") {
									data["item"][fname] = NCMMPubUtil.htmlspecialchars2sqliteex(data["item"][fname]);
								}
								return data["item"][fname];
							} else {
								if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
									return 0;
								} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
									return 0.0;
								} else {
									return "";
								}
							}
						});
						valueMapList_body.push(values);
					}
				}
				//				$alert("valueMapList_body=" + $jsonToString(valueMapList_body));
				//先写入表体
				$app.replaceBatch({
					db: NCMMPubUtil.DatabaseName,
					table: VOClassBody.prototype.getTableName(),
					fields: fields_body,
					values: valueMapList_body,
					callback: function(args_null, args) {
						if(args.code == '1000' || args.code == 1000) {
							//表体写入成功后写表头
							$app.replaceBatch({
								db: NCMMPubUtil.DatabaseName,
								table: VOClassHead.prototype.getTableName(),
								fields: fields_head,
								values: valueMapList_head,
								callback: function(args_null, args) {
									var currentDate = NCMMHttpUtil.getCurrentLocalTime();
									//3. 更新本地app_mappinghead表中下载记录数和下载时间
									NCMMDBUtil.updateMappingTable(VOClassHead, sum, currentDate, String(100));
									//4. 刷新页面显示数据
									if(needReloadrow && needReloadrow == true) {
										reloadSingleRow(VOClassHead);
									}

									NCMMDBUtil.hideLoading();
									$toast("【" + appbillname + "】下载了【" + sum + "】条数据。");
									if(callback) {
										callback();
									}
								}
							});

						} else {
							NCMMDBUtil.hideLoading();
							$js.toast("【" + appbillname + "】下载异常");
							if(callback) {
								callback();
							}
						}
					}
				});
			}
		}, function(_NULL, args) {
			NCMMDBUtil.hideLoading();
			$js.toast('下载数据出错！');
			if(callback) {
				callback();
			}
		});
	};

	NCMMDBUtil.processDocDataFromNC2017 = function(VOClass, appbillname, condition, groupings, index, callback, needReloadrow) {
		if(index == 0) {
			if(window.showLoading) {
				$app.showLoadingBar({
					msg: "正在下载……"
				});
				window.showLoading = false;
			}
			// 清空本地表记录
			NCMMDBUtil.clearAllOldData(VOClass, null);
		} else {
			condition["startRowNumber"] = Number(groupings[index][0]);
			condition["endRowNumber"] = Number(groupings[index][1]);
		}

		// 从NC端获取档案数据
		NCMMDBUtil.loadNCBaseData2(VOClass, null, NCMMHttpUtil.BDDOC, condition, function(caller, data) {
			// 获得档案数据	
			var result = $stringToJSON((($stringToJSON(data.http_result)).response || {}));
			//			$alert("result=" + $jsonToString(result));
			if(result != null) {
				// 如果没有信息直接返回
				var billinfos = result.billinfos || {};
				var props = Object.getOwnPropertyNames(billinfos) || [];
				if(props.length == 0) {
					if($isJSONObject(result)) {
						NCMMDBUtil.hideLoading();
						$toast("没有下载到【" + appbillname + "】");
					} else {
						NCMMDBUtil.hideLoading();
						$toast(result);
					}
					return;
				}
				// 获得条数
				var sum = NCMMDBUtil.getBillCount(result);
				//$alert("sum=[" + sum + "]:" + (result.pageinfos).startRowNumber + "~~" + (result.pageinfos).endRowNumber);
				// 第一次获得下载分组
				if(index == 0) {
					groupings = NCMMDBUtil.getGroup(sum, 5000);
				}
				// TODO 执行拓展服务insert操作
				var attrs = VOClass.prototype.getAllAttrs();
				var fields = attrs.map(function(row, i, arr) {
					return row.name;
				});

				var valueMapList = Array.prototype.map.call(result.billinfos, function(row, i, arr) {
					var data = $stringToJSON((row || {}));
					var values = attrs.map(function(attr, i, array) {
						var fname = attr.name;
						if(!!data["billinfo"][fname]) {
							if(data["billinfo"][fname] != "" && typeof(data["billinfo"][fname]) == "string") {
								data["billinfo"][fname] = NCMMPubUtil.htmlspecialchars2sqliteex(data["billinfo"][fname]);
							}
							return data["billinfo"][fname];
						} else {
							if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
								return 0;
							} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
								return 0.0;
							} else {
								return "";
							}
						}
					});
					return values;
				});
				//												$alert("valueMapList=" + $jsonToString(valueMapList));
				$app.replaceBatch({
					db: NCMMPubUtil.DatabaseName,
					table: VOClass.prototype.getTableName(),
					fields: fields,
					values: valueMapList,
					callback: function(args_null, args) {
						if(args.code == '1000' || args.code == 1000) {
							var currentDate = NCMMHttpUtil.getCurrentLocalTime();
							//3. 更新本地app_mappinghead表中下载记录数和下载时间
							NCMMDBUtil.updateMappingTable(VOClass, groupings[index][1], currentDate, String(groupings[index][2]));
							//4. 刷新页面显示数据
							if(needReloadrow && needReloadrow == true) {
								reloadSingleRow(VOClass);
							}

							var maxLength = groupings.length;
							var i = index + 1;
							if(i != groupings.length) {
								(function(VOClass, appbillname, condition, groupings, i, callback, needReloadrow) {
									NCMMDBUtil.processDocDataFromNC2017(VOClass, appbillname, condition, groupings, i, callback, needReloadrow);
								})(VOClass, appbillname, condition, groupings, i, callback, needReloadrow);
							} else {
								NCMMDBUtil.hideLoading();
								$toast("【" + appbillname + "】下载了【" + sum + "】条数据。");
								if(callback) {
									callback();
								}
							}
						} else {
							NCMMDBUtil.hideLoading();
							$js.toast("【" + appbillname + "】下载异常");
							if(callback) {
								callback();
							}
						}
					}
				});
			}
		}, function(_NULL, args) {
			NCMMDBUtil.hideLoading();
			$js.toast('下载数据出错！');
			if(callback) {
				callback();
			}
		});
	};

	/**
	 * 获得分组
	 */
	NCMMDBUtil.getGroup = function(sum, base) {
		if(base == null)
			base = 5000;

		var groups = [];
		var gropuNums = parseInt(sum / base);
		if(gropuNums == 0) {
			var group = [];
			group[0] = 1;
			group[1] = sum;
			group[2] = 100;
			groups.push(group);
		} else {
			var step = parseInt(100.00 * base / sum);
			for(var i = 0; i <= gropuNums; i++) {
				var group = [];
				if(i != gropuNums) {
					group[0] = 1 + (i * base);
					group[1] = (i + 1) * base;
					group[2] = step * (i + 1);
				} else {
					group[0] = 1 + (i * base);
					group[1] = sum;
					group[2] = 100;
				}
				groups.push(group);
			};
		}
		return groups;
	};

	//根据档案主键获得本地表中档案vo
	NCMMDBUtil.getVOByPK = function(VOClass, pkname, pkvalue) {
		var queryvo = null;
		var wheresql = "where " + pkname + " IN('" + pkvalue + "')";
		//				$alert("wheresql="+ wheresql);
		queryvo = NCMMPubUtil.queryVONoCallback(VOClass, wheresql);
		//				$alert("queryvo="+$jsonToString(queryvo));
		return queryvo;
	};

	//根据档案主键获得本地表中档案vos
	NCMMDBUtil.getVOsByPK = function(VOClass, pkname, pkvalue) {
		var queryvos = null;
		var tablename = VOClass.prototype.getTableName();
		var wheresql = "select * from " + tablename + " where " + pkname + " IN('" + pkvalue + "')";
		queryvos = NCMMPubUtil.queryBySQL(wheresql);

		return queryvos;
	};

	//根据单据行获得本地表中单据vo
	NCMMDBUtil.getVOsByBillcodeRow = function(VOClass, billcodename, billcodevalue, rownoname, rownovalue) {
		var queryvos = null;
		var wheresql = "where " + billcodename + " IN('" + billcodevalue + "') AND " + rownoname + " IN('" + rownovalue + "')";
		//		$alert("wheresql="+ wheresql);
		queryvos = NCMMPubUtil.queryVONoCallback(VOClass, wheresql);
		//		$alert("queryvos="+queryvos);
		if(queryvos) {
			if(typeof(queryvos) == "string") {
				queryvos = $stringToJSON(queryvos);
			}
			if($isJSONArray(queryvos) && queryvos.length > 0) {
				return queryvos;
			}
		} else {
			return null;
		}
	};

	//初始化对照表数据
	NCMMDBUtil.initMappingTable = function() {
		if($cache.read("mappingdataloaded") == "true")
			return;

		NCMMPubUtil.dropTable(AppmappingheadVO);
		NCMMPubUtil.createTable(AppmappingheadVO);

		//为优化性能，改为批量插入
		NCMMPubUtil.insertBatchVO(AppmappingheadVO, NCMMDBUtil.MAPPINGHEADDATA);

		//测试代码
		/*		 
		{
		var sql = "select count(*) ct from " + AppmappingheadVO.prototype.getTableName();
		var param = {
			db : NCMMPubUtil.DatabaseName,
			sql : sql
		}
		var data = $sqlite.query(param);
		if($isPlainObject(data) == false)
			data = $stringToJSON(data);
		$alert("data=" + $jsonToString(data));
		}
		*/
		$cache.write("mappingdataloaded", "true");
	}

	NCMMDBUtil.MAPPINGHEADDATA = [
		//	{
		//		"pk_map": 1,
		//		"ncbillcode": "PDAFACTORY",
		//		"apptablename": "app_org_factory",
		//		"appbillname": "工厂",
		//		"location": "SINGLE",
		//		"datatype": "BD"
		//	}, 
		{
			"pk_map": 2,
			"ncbillcode": "PDASTOCKORG",
			"apptablename": "app_org_stockorg",
			"appbillname": "库存组织",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 3,
			"ncbillcode": "PDASTORDOC",
			"apptablename": "app_bd_stordoc",
			"appbillname": "仓库",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 4,
			"ncbillcode": "PDAMATERIAL",
			"apptablename": "app_bd_material",
			"appbillname": "物料",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 5,
			"ncbillcode": "PDABATCH",
			"apptablename": "app_scm_batchcode",
			"appbillname": "批次",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 6,
			"ncbillcode": "PDAMEASDOC",
			"apptablename": "app_bd_measdoc",
			"appbillname": "计量单位",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 7,
			"ncbillcode": "PDAWK",
			"apptablename": "app_bd_wk",
			"appbillname": "工作中心",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 8,
			"ncbillcode": "PDADEPT",
			"apptablename": "app_org_dept",
			"appbillname": "部门",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 9,
			"ncbillcode": "PDABARCODEDICT",
			"apptablename": "app_bcbd_barcodedict",
			"appbillname": "条码字典",
			"location": "HEAD",
			"datatype": "BD"
		}, {
			"pk_map": 10,
			"ncbillcode": "PDABARCODEDICT",
			"apptablename": "app_bcbd_barcodedict_b",
			"appbillname": "条码字典",
			"location": "BODY",
			"datatype": "BD"
		}, {
			"pk_map": 11,
			"ncbillcode": "PDABARCODERULE",
			"apptablename": "app_bcbd_barcoderule",
			"appbillname": "条码规则",
			"location": "HEAD",
			"datatype": "BD"
		}, {
			"pk_map": 12,
			"ncbillcode": "PDABARCODERULE",
			"apptablename": "app_bcbd_barcoderule_b",
			"appbillname": "条码规则",
			"location": "BODY",
			"datatype": "BD"
		}, {
			"pk_map": 13,
			"ncbillcode": "PDASUPPLIER",
			"apptablename": "app_supplier",
			"appbillname": "供应商",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 14,
			"ncbillcode": "PDACUSTOMER",
			"apptablename": "app_customer",
			"appbillname": "客户",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 15,
			"ncbillcode": "PDARACK",
			"apptablename": "app_bd_rack",
			"appbillname": "货位",
			"location": "SINGLE",
			"datatype": "BD"
		}, {
			"pk_map": 23,
			"ncbillcode": "23query",
			"apptablename": "app_po_arriveorder",
			"appbillname": "销售订单主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 24,
			"ncbillcode": "23query",
			"apptablename": "app_po_arriveorder_b",
			"appbillname": "销售订单子实体",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 25,
			"ncbillcode": "4331query",
			"apptablename": "so_delivery",
			"appbillname": "到货单主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 26,
			"ncbillcode": "4331query",
			"apptablename": "so_delivery_b",
			"appbillname": "到货单子实体",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 27,
			"ncbillcode": "46query",
			"apptablename": "pu_finprodin",
			"appbillname": "产成品入库主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 28,
			"ncbillcode": "46query",
			"apptablename": "pu_finprodin_b",
			"appbillname": "产成品入库子实体",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 29,
			"ncbillcode": "4Dquery",
			"apptablename": "ic_materialout",
			"appbillname": "材料出库主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 30,
			"ncbillcode": "4Dquery",
			"apptablename": "ic_materialout",
			"appbillname": "材料出库子实体",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 31,
			"ncbillcode": "4Rquery",
			"apptablename": "ic_invcount",
			"appbillname": "库存盘点主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 32,
			"ncbillcode": "4Rquery",
			"apptablename": "ic_invcount",
			"appbillname": "库存盘点子实体",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 33,
			"ncbillcode": "5Xquery",
			"apptablename": "to_trans",
			"appbillname": "调拨订单",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 34,
			"ncbillcode": "5Xquery",
			"apptablename": "to_trans",
			"appbillname": "调拨订单",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 35,
			"ncbillcode": "30query",
			"apptablename": "so_so",
			"appbillname": "销售订单",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 36,
			"ncbillcode": "30query",
			"apptablename": "so_so",
			"appbillname": "销售订单",
			"location": "BODY",
			"datatype": "BILL"
	    }, {
			"pk_map": 37,
			"ncbillcode": "4Y_4Equery",
			"apptablename": "to_transins",
			"appbillname": "调拨出库主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 38,
			"ncbillcode": "4Y_4Equery",
			"apptablename": "to_transins",
			"appbillname": "调拨出库子实体",
			"location": "BODY",
			"datatype": "BILL"
		},{
			"pk_map": 39,
			"ncbillcode": "4Kquery",
			"apptablename": "ic_whstrans",
			"appbillname": "转库单主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 40,
			"ncbillcode": "4Kquery",
			"apptablename": "ic_whstrans",
			"appbillname": "转库单子实体",
			"location": "BODY",
			"datatype": "BILL"
		},{
			"pk_map": 41,
			"ncbillcode": "21query",
			"apptablename": "po_arrive",
			"appbillname": "采购到货主实体",
			"location": "HEAD",
			"datatype": "BILL"
		}, {
			"pk_map": 42,
			"ncbillcode": "21query",
			"apptablename": "po_arrive",
			"appbillname": "采购到货子实体",
			"location": "BODY",
			"datatype": "BILL"
		}, {
			"pk_map": 43,
			"ncbillcode": "PDASTORSTATE",
			"apptablename": "app_ic_storestate",
			"appbillname": "库存状态",
			"location": "SINGLE",
			"datatype": "BD"
		}
		
	];

} catch(e) {
	$e(e);
}